{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "35e46ca6-eaf2-429f-8767-bff39c7c0019",
   "metadata": {},
   "source": [
    "# Pandas层次化索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f325437e-61b1-4b4c-98a5-e00af27b3221",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b451e1ce-3e39-44ff-8d93-2b8992c0c11a",
   "metadata": {},
   "source": [
    "## 1. 创建多层索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce8b8e0f-f2a5-4ba5-b09e-b2129e921741",
   "metadata": {},
   "source": [
    "1）隐式构造\n",
    "最常见的做法是给DataFrame构造函数的index参数传递两个或更多的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "09dc158b-52bc-4469-a754-3befca8a37c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>66</td>\n",
       "      <td>10</td>\n",
       "      <td>24</td>\n",
       "      <td>0</td>\n",
       "      <td>56</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>33</td>\n",
       "      <td>98</td>\n",
       "      <td>26</td>\n",
       "      <td>35</td>\n",
       "      <td>12</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>25</td>\n",
       "      <td>28</td>\n",
       "      <td>26</td>\n",
       "      <td>45</td>\n",
       "      <td>54</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>赵六</th>\n",
       "      <td>94</td>\n",
       "      <td>50</td>\n",
       "      <td>19</td>\n",
       "      <td>44</td>\n",
       "      <td>27</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>孙七</th>\n",
       "      <td>85</td>\n",
       "      <td>25</td>\n",
       "      <td>12</td>\n",
       "      <td>40</td>\n",
       "      <td>8</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王八</th>\n",
       "      <td>78</td>\n",
       "      <td>54</td>\n",
       "      <td>90</td>\n",
       "      <td>76</td>\n",
       "      <td>10</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  66  10  24   0  56  45\n",
       "   李四  33  98  26  35  12   4\n",
       "   王五  25  28  26  45  54  99\n",
       "2班 赵六  94  50  19  44  27  41\n",
       "   孙七  85  25  12  40   8   6\n",
       "   王八  78  54  90  76  10  30"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,100,size=(6,6))\n",
    "\n",
    "index=[\n",
    "    ['1班','1班','1班','2班','2班','2班'],\n",
    "    ['张三','李四','王五','赵六','孙七','王八']\n",
    "]\n",
    "\n",
    "columns=[\n",
    "    ['期中','期中','期中','期末','期末','期末'],\n",
    "    ['语文','数学','英语','语文','数学','英语']\n",
    "]\n",
    "\n",
    "df=pd.DataFrame(data=data,index=index,columns=columns)\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56a06805-77db-43a3-bb09-4455f6d1b94f",
   "metadata": {},
   "source": [
    "- Series也可以创建多层索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "79ada518-d3ed-4795-8994-ac8a5cca6cee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  张三    15\n",
       "    李四    16\n",
       "    王五    82\n",
       "2班  赵六    12\n",
       "    孙七    16\n",
       "    王八    75\n",
       "dtype: int32"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=np.random.randint(0,100,size=6)\n",
    "index=[\n",
    "    ['1班','1班','1班','2班','2班','2班'],\n",
    "    ['张三','李四','王五','赵六','孙七','王八']\n",
    "]\n",
    "\n",
    "s=pd.Series(data=data,index=index)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "945552ae-a49f-4fba-9127-8e6b65c3865e",
   "metadata": {},
   "source": [
    "2)显示构造pd.Multilndex"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3738df1e-9f36-41c4-bfa9-c19b3123e4b9",
   "metadata": {},
   "source": [
    "- 使用数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "37d576c7-cae2-4e14-acb2-184d6252a5b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>80</td>\n",
       "      <td>43</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>80</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>49</td>\n",
       "      <td>92</td>\n",
       "      <td>84</td>\n",
       "      <td>8</td>\n",
       "      <td>79</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>70</td>\n",
       "      <td>26</td>\n",
       "      <td>79</td>\n",
       "      <td>70</td>\n",
       "      <td>96</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>赵六</th>\n",
       "      <td>22</td>\n",
       "      <td>94</td>\n",
       "      <td>73</td>\n",
       "      <td>61</td>\n",
       "      <td>99</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>孙七</th>\n",
       "      <td>72</td>\n",
       "      <td>37</td>\n",
       "      <td>34</td>\n",
       "      <td>41</td>\n",
       "      <td>97</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王八</th>\n",
       "      <td>50</td>\n",
       "      <td>31</td>\n",
       "      <td>78</td>\n",
       "      <td>71</td>\n",
       "      <td>48</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  80  43   0  55  80  86\n",
       "   李四  49  92  84   8  79  82\n",
       "   王五  70  26  79  70  96  28\n",
       "2班 赵六  22  94  73  61  99  86\n",
       "   孙七  72  37  34  41  97  48\n",
       "   王八  50  31  78  71  48  31"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,100,size=(6,6))\n",
    "\n",
    "index=pd.MultiIndex.from_arrays([\n",
    "    ['1班','1班','1班','2班','2班','2班'],\n",
    "    ['张三','李四','王五','赵六','孙七','王八']\n",
    "])\n",
    "\n",
    "columns=[\n",
    "    ['期中','期中','期中','期末','期末','期末'],\n",
    "    ['语文','数学','英语','语文','数学','英语']\n",
    "]\n",
    "\n",
    "df=pd.DataFrame(data=data,index=index,columns=columns)\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "785ac413-e4e2-4c32-bd58-92f3e47c3924",
   "metadata": {},
   "source": [
    "- 使用tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ea54bf7e-f640-4e80-a51b-4ab75231fe02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">一班</th>\n",
       "      <th>张三</th>\n",
       "      <td>87</td>\n",
       "      <td>77</td>\n",
       "      <td>25</td>\n",
       "      <td>49</td>\n",
       "      <td>18</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>23</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>94</td>\n",
       "      <td>66</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>15</td>\n",
       "      <td>56</td>\n",
       "      <td>18</td>\n",
       "      <td>48</td>\n",
       "      <td>33</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">二班</th>\n",
       "      <th>赵六</th>\n",
       "      <td>89</td>\n",
       "      <td>84</td>\n",
       "      <td>12</td>\n",
       "      <td>56</td>\n",
       "      <td>82</td>\n",
       "      <td>59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>孙七</th>\n",
       "      <td>43</td>\n",
       "      <td>80</td>\n",
       "      <td>31</td>\n",
       "      <td>84</td>\n",
       "      <td>33</td>\n",
       "      <td>56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王八</th>\n",
       "      <td>95</td>\n",
       "      <td>76</td>\n",
       "      <td>29</td>\n",
       "      <td>4</td>\n",
       "      <td>57</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "一班 张三  87  77  25  49  18  79\n",
       "   李四  23  12   3  94  66  36\n",
       "   王五  15  56  18  48  33  39\n",
       "二班 赵六  89  84  12  56  82  59\n",
       "   孙七  43  80  31  84  33  56\n",
       "   王八  95  76  29   4  57  57"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,100,size=(6,6))\n",
    "index=pd.MultiIndex.from_tuples(\n",
    "    (\n",
    "        ('一班','张三'),('一班','李四'),('一班','王五'),\n",
    "        ('二班','赵六'),('二班','孙七'),('二班','王八')\n",
    "    )\n",
    ")\n",
    "\n",
    "columns=[\n",
    "    ['期中','期中','期中','期末','期末','期末'],\n",
    "    ['语文','数学','英语','语文','数学','英语']\n",
    "]\n",
    "\n",
    "df=pd.DataFrame(data=data,index=index,columns=columns)\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce428caa-11a7-405c-b557-34099085daf2",
   "metadata": {},
   "source": [
    "- 使用product\n",
    "- - 笛卡尔积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e8c4b936-527c-4e2c-bff1-4c1b60fe2965",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>27</td>\n",
       "      <td>43</td>\n",
       "      <td>72</td>\n",
       "      <td>16</td>\n",
       "      <td>74</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>81</td>\n",
       "      <td>93</td>\n",
       "      <td>7</td>\n",
       "      <td>48</td>\n",
       "      <td>73</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>36</td>\n",
       "      <td>92</td>\n",
       "      <td>33</td>\n",
       "      <td>75</td>\n",
       "      <td>36</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>3</td>\n",
       "      <td>42</td>\n",
       "      <td>6</td>\n",
       "      <td>92</td>\n",
       "      <td>42</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>56</td>\n",
       "      <td>68</td>\n",
       "      <td>51</td>\n",
       "      <td>87</td>\n",
       "      <td>71</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>15</td>\n",
       "      <td>20</td>\n",
       "      <td>34</td>\n",
       "      <td>37</td>\n",
       "      <td>19</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  27  43  72  16  74  99\n",
       "   李四  81  93   7  48  73  96\n",
       "   王五  36  92  33  75  36  87\n",
       "2班 张三   3  42   6  92  42  38\n",
       "   李四  56  68  51  87  71  17\n",
       "   王五  15  20  34  37  19  11"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,100,size=(6,6))\n",
    "\n",
    "\n",
    "#笛卡尔积:{a,b}{c,d}=>{a,c}{a,d}{b,c}{b,d}\n",
    "\n",
    "index=pd.MultiIndex.from_product([\n",
    "    ['1班','2班'],\n",
    "    ['张三','李四','王五']\n",
    "])\n",
    "\n",
    "columns=[\n",
    "    ['期中','期中','期中','期末','期末','期末'],\n",
    "    ['语文','数学','英语','语文','数学','英语']\n",
    "]\n",
    "\n",
    "df=pd.DataFrame(data=data,index=index,columns=columns)\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5ce15a0-ce97-4512-9ca8-8a326025d4e5",
   "metadata": {},
   "source": [
    "## 2.多层索引对象的索引与切片操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "667870d4-4c3d-4748-b9c4-37918c77308e",
   "metadata": {},
   "source": [
    "1）Series的2操作\n",
    "- 对于Series来说，直接使用中括号[]与使用.loc()完全一样"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56ac5127-9dd9-490c-b9a2-d240e852269c",
   "metadata": {},
   "source": [
    "（1）索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4bfe1822-0bd4-4caa-a7c4-30884881bf29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  张三    15\n",
       "    李四    16\n",
       "    王五    82\n",
       "2班  赵六    12\n",
       "    孙七    16\n",
       "    王八    75\n",
       "dtype: int32"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5307a914-e1e2-456c-a9d0-94ac420ef299",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_33896\\1944261373.py:15: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  s[0] # 直接返回值\n",
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_33896\\1944261373.py:16: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  s[1]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1班  李四    16\n",
       "    王五    82\n",
       "dtype: int32"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示索引\n",
    "s['1班']\n",
    "s.loc['1班']\n",
    "\n",
    "s[['1班']]\n",
    "s[['1班','2班']]\n",
    "\n",
    "s['1班']['张三']\n",
    "s.loc['1班']['张三']\n",
    "s.loc['1班','张三']\n",
    "s['1班','张三']\n",
    "\n",
    "\n",
    "# 隐式索引\n",
    "s[0] # 直接返回值\n",
    "s[1]\n",
    "s.iloc[1]\n",
    "s.iloc[[1,2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e004ccf2-d68b-40b1-8a27-09160833875e",
   "metadata": {},
   "source": [
    "(2)切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8506b0d4-b180-4dcd-ba61-fd35ba6962b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  张三    15\n",
       "    李四    16\n",
       "    王五    82\n",
       "2班  赵六    12\n",
       "    孙七    16\n",
       "    王八    75\n",
       "dtype: int32"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "87988aec-ae14-49e9-a8ce-845f6fc38df0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  李四    16\n",
       "    王五    82\n",
       "2班  赵六    12\n",
       "    孙七    16\n",
       "dtype: int32"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显式切片\n",
    "s['1班':'2班']# 左闭右闭\n",
    "s.loc['1班':'2班']\n",
    "s['1班'][:]\n",
    "s['1班']['张三':'李四']\n",
    "\n",
    "# 隐式索引\n",
    "s[1:5] # 左闭右开\n",
    "s.iloc[1:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b31775fc-e30c-4a54-9bcd-066d078a020e",
   "metadata": {},
   "source": [
    "2)DataFrame的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3794cc3b-04ef-4672-9f71-abb414dfdbcc",
   "metadata": {},
   "source": [
    "(1)索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7cc6f42d-ec73-4284-8ba4-e003beb5a664",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>27</td>\n",
       "      <td>43</td>\n",
       "      <td>72</td>\n",
       "      <td>16</td>\n",
       "      <td>74</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>81</td>\n",
       "      <td>93</td>\n",
       "      <td>7</td>\n",
       "      <td>48</td>\n",
       "      <td>73</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>36</td>\n",
       "      <td>92</td>\n",
       "      <td>33</td>\n",
       "      <td>75</td>\n",
       "      <td>36</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>3</td>\n",
       "      <td>42</td>\n",
       "      <td>6</td>\n",
       "      <td>92</td>\n",
       "      <td>42</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>56</td>\n",
       "      <td>68</td>\n",
       "      <td>51</td>\n",
       "      <td>87</td>\n",
       "      <td>71</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>15</td>\n",
       "      <td>20</td>\n",
       "      <td>34</td>\n",
       "      <td>37</td>\n",
       "      <td>19</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  27  43  72  16  74  99\n",
       "   李四  81  93   7  48  73  96\n",
       "   王五  36  92  33  75  36  87\n",
       "2班 张三   3  42   6  92  42  38\n",
       "   李四  56  68  51  87  71  17\n",
       "   王五  15  20  34  37  19  11"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9e178ff1-18a5-4e69-9e05-e6280d729aa8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_33896\\253858888.py:3: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  df['期中']['数学']['1班'][0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "np.int32(43)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取元素\n",
    "df['期中']['数学']['1班']['张三']\n",
    "df['期中']['数学']['1班'][0]\n",
    "df.iloc[0,1]\n",
    "\n",
    "df.loc[('1班','张三'),('期中','数学')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5538998d-9d04-4ca8-980c-11f7da52d3d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1班</th>\n",
       "      <th>李四</th>\n",
       "      <td>81</td>\n",
       "      <td>93</td>\n",
       "      <td>7</td>\n",
       "      <td>48</td>\n",
       "      <td>73</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>3</td>\n",
       "      <td>42</td>\n",
       "      <td>6</td>\n",
       "      <td>92</td>\n",
       "      <td>42</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>56</td>\n",
       "      <td>68</td>\n",
       "      <td>51</td>\n",
       "      <td>87</td>\n",
       "      <td>71</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1班</th>\n",
       "      <th>王五</th>\n",
       "      <td>36</td>\n",
       "      <td>92</td>\n",
       "      <td>33</td>\n",
       "      <td>75</td>\n",
       "      <td>36</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 李四  81  93   7  48  73  96\n",
       "2班 张三   3  42   6  92  42  38\n",
       "   李四  56  68  51  87  71  17\n",
       "1班 王五  36  92  33  75  36  87"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列索引\n",
    "df['期中']\n",
    "df['期中']['数学']\n",
    "# 保留DataFrmae类型\n",
    "df['期中'][['数学']]\n",
    "\n",
    "df['期中','数学']\n",
    "df.期中.数学\n",
    "\n",
    "df.iloc[:,2]\n",
    "df.iloc[:,[0,2,1]]\n",
    "df.loc[:,('期中','数学')]\n",
    "\n",
    "# 行索引\n",
    "df.loc['2班']\n",
    "df.loc['2班'].loc['张三']\n",
    "df.loc['2班','张三']\n",
    "df.loc[('2班','张三')]\n",
    "\n",
    "df.iloc[0]\n",
    "df.iloc[[1]]\n",
    "df.iloc[[1,3,4,2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12187119-0d69-4363-ab45-2d1cf2af0192",
   "metadata": {},
   "source": [
    "(2)切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f0686272-ea2e-4e0a-bbfc-4825379e68f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>27</td>\n",
       "      <td>43</td>\n",
       "      <td>72</td>\n",
       "      <td>16</td>\n",
       "      <td>74</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>81</td>\n",
       "      <td>93</td>\n",
       "      <td>7</td>\n",
       "      <td>48</td>\n",
       "      <td>73</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>36</td>\n",
       "      <td>92</td>\n",
       "      <td>33</td>\n",
       "      <td>75</td>\n",
       "      <td>36</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>3</td>\n",
       "      <td>42</td>\n",
       "      <td>6</td>\n",
       "      <td>92</td>\n",
       "      <td>42</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>56</td>\n",
       "      <td>68</td>\n",
       "      <td>51</td>\n",
       "      <td>87</td>\n",
       "      <td>71</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>15</td>\n",
       "      <td>20</td>\n",
       "      <td>34</td>\n",
       "      <td>37</td>\n",
       "      <td>19</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  27  43  72  16  74  99\n",
       "   李四  81  93   7  48  73  96\n",
       "   王五  36  92  33  75  36  87\n",
       "2班 张三   3  42   6  92  42  38\n",
       "   李四  56  68  51  87  71  17\n",
       "   王五  15  20  34  37  19  11"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行切片\n",
    "df.iloc[1:5] # 左开右闭\n",
    "df.loc[('1班','李四'):('2班','王五')]#左闭右闭\n",
    "df.loc['1班':'2班']\n",
    "\n",
    "# 列切片\n",
    "# 需要先对行做切片\n",
    "df.iloc[:,1:5]\n",
    "df.loc[:,'期中':'期末']\n",
    "# df.loc[:,('期中','数学'):('期末','数学')]# 报错 列索引不能使用该方法做切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ee9f097-b84d-4b30-b7b8-8b5f18e860c9",
   "metadata": {},
   "source": [
    "## 3.索引的堆叠"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e4feed2-1464-45d2-bc48-6d42ee24ec56",
   "metadata": {},
   "source": [
    "- stack()\n",
    "- unstack()\n",
    "使用stack的时候，level等于哪一个，哪一个就消失，出现在行里"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "be78a00d-3ccf-4f63-8d3a-c3f546f1b034",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>33</td>\n",
       "      <td>44</td>\n",
       "      <td>66</td>\n",
       "      <td>62</td>\n",
       "      <td>57</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>39</td>\n",
       "      <td>12</td>\n",
       "      <td>54</td>\n",
       "      <td>42</td>\n",
       "      <td>71</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>23</td>\n",
       "      <td>30</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>43</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>61</td>\n",
       "      <td>85</td>\n",
       "      <td>94</td>\n",
       "      <td>77</td>\n",
       "      <td>49</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>84</td>\n",
       "      <td>3</td>\n",
       "      <td>36</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>73</td>\n",
       "      <td>3</td>\n",
       "      <td>15</td>\n",
       "      <td>20</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  33  44  66  62  57  70\n",
       "   李四  39  12  54  42  71  10\n",
       "   王五  23  30  10  11  43  60\n",
       "2班 张三  61  85  94  77  49  25\n",
       "   李四  84   3  36  89  21  32\n",
       "   王五  66  73   3  15  20  80"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,100,size=(6,6))\n",
    "\n",
    "\n",
    "#笛卡尔积:{a,b}{c,d}=>{a,c}{a,d}{b,c}{b,d}\n",
    "\n",
    "index=pd.MultiIndex.from_product([\n",
    "    ['1班','2班'],\n",
    "    ['张三','李四','王五']\n",
    "])\n",
    "\n",
    "columns=[\n",
    "    ['期中','期中','期中','期末','期末','期末'],\n",
    "    ['语文','数学','英语','语文','数学','英语']\n",
    "]\n",
    "\n",
    "df=pd.DataFrame(data=data,index=index,columns=columns)\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9351a6c7-2180-49f1-bab8-f66963bd6e4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_33896\\2035216694.py:2: FutureWarning: The previous implementation of stack is deprecated and will be removed in a future version of pandas. See the What's New notes for pandas 2.1.0 for details. Specify future_stack=True to adopt the new implementation and silence this warning.\n",
      "  df.stack() # 默认将最里层的列索引变为行索引\n",
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_33896\\2035216694.py:4: FutureWarning: The previous implementation of stack is deprecated and will be removed in a future version of pandas. See the What's New notes for pandas 2.1.0 for details. Specify future_stack=True to adopt the new implementation and silence this warning.\n",
      "  df.stack(level=1)\n",
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_33896\\2035216694.py:5: FutureWarning: The previous implementation of stack is deprecated and will be removed in a future version of pandas. See the What's New notes for pandas 2.1.0 for details. Specify future_stack=True to adopt the new implementation and silence this warning.\n",
      "  df.stack(level=-1)\n",
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_33896\\2035216694.py:6: FutureWarning: The previous implementation of stack is deprecated and will be removed in a future version of pandas. See the What's New notes for pandas 2.1.0 for details. Specify future_stack=True to adopt the new implementation and silence this warning.\n",
      "  df.stack(level=0)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"6\" valign=\"top\">1班</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">张三</th>\n",
       "      <th>期中</th>\n",
       "      <td>33</td>\n",
       "      <td>44</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>62</td>\n",
       "      <td>57</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">李四</th>\n",
       "      <th>期中</th>\n",
       "      <td>39</td>\n",
       "      <td>12</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>42</td>\n",
       "      <td>71</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">王五</th>\n",
       "      <th>期中</th>\n",
       "      <td>23</td>\n",
       "      <td>30</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>11</td>\n",
       "      <td>43</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"6\" valign=\"top\">2班</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">张三</th>\n",
       "      <th>期中</th>\n",
       "      <td>61</td>\n",
       "      <td>85</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>77</td>\n",
       "      <td>49</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">李四</th>\n",
       "      <th>期中</th>\n",
       "      <td>84</td>\n",
       "      <td>3</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">王五</th>\n",
       "      <th>期中</th>\n",
       "      <td>66</td>\n",
       "      <td>73</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>15</td>\n",
       "      <td>20</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          语文  数学  英语\n",
       "1班 张三 期中  33  44  66\n",
       "      期末  62  57  70\n",
       "   李四 期中  39  12  54\n",
       "      期末  42  71  10\n",
       "   王五 期中  23  30  10\n",
       "      期末  11  43  60\n",
       "2班 张三 期中  61  85  94\n",
       "      期末  77  49  25\n",
       "   李四 期中  84   3  36\n",
       "      期末  89  21  32\n",
       "   王五 期中  66  73   3\n",
       "      期末  15  20  80"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将列索引变成行索引\n",
    "df.stack() # 默认将最里层的列索引变为行索引\n",
    "\n",
    "df.stack(level=1)\n",
    "df.stack(level=-1)\n",
    "df.stack(level=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2d227bb6-0f16-4803-bf96-33f4dfc1a146",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"6\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"6\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">语文</th>\n",
       "      <th colspan=\"2\" halign=\"left\">数学</th>\n",
       "      <th colspan=\"2\" halign=\"left\">英语</th>\n",
       "      <th colspan=\"2\" halign=\"left\">语文</th>\n",
       "      <th colspan=\"2\" halign=\"left\">数学</th>\n",
       "      <th colspan=\"2\" halign=\"left\">英语</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>1班</th>\n",
       "      <th>2班</th>\n",
       "      <th>1班</th>\n",
       "      <th>2班</th>\n",
       "      <th>1班</th>\n",
       "      <th>2班</th>\n",
       "      <th>1班</th>\n",
       "      <th>2班</th>\n",
       "      <th>1班</th>\n",
       "      <th>2班</th>\n",
       "      <th>1班</th>\n",
       "      <th>2班</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>33</td>\n",
       "      <td>61</td>\n",
       "      <td>44</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>94</td>\n",
       "      <td>62</td>\n",
       "      <td>77</td>\n",
       "      <td>57</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>39</td>\n",
       "      <td>84</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>54</td>\n",
       "      <td>36</td>\n",
       "      <td>42</td>\n",
       "      <td>89</td>\n",
       "      <td>71</td>\n",
       "      <td>21</td>\n",
       "      <td>10</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>23</td>\n",
       "      <td>66</td>\n",
       "      <td>30</td>\n",
       "      <td>73</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "      <td>11</td>\n",
       "      <td>15</td>\n",
       "      <td>43</td>\n",
       "      <td>20</td>\n",
       "      <td>60</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    期中                      期末                    \n",
       "    语文      数学      英语      语文      数学      英语    \n",
       "    1班  2班  1班  2班  1班  2班  1班  2班  1班  2班  1班  2班\n",
       "张三  33  61  44  85  66  94  62  77  57  49  70  25\n",
       "李四  39  84  12   3  54  36  42  89  71  21  10  32\n",
       "王五  23  66  30  73  10   3  11  15  43  20  60  80"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将行索引变成列索引\n",
    "df.unstack()\n",
    "df.unstack(level=1)\n",
    "df.unstack(level=0)\n",
    "df.unstack(level=0,fill_value=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a0da983-75b3-454f-b149-b5f0e4979f39",
   "metadata": {},
   "source": [
    "## 4.聚合操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef3ca14a-2a1f-4da3-800a-4c347a699b14",
   "metadata": {},
   "source": [
    "DataFrame聚合操作\n",
    "- 求和\n",
    "- 平均值\n",
    "- 最大值\n",
    "- 最小值等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9196305b-0ff6-4d78-856c-8e4418aa9843",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>33</td>\n",
       "      <td>44</td>\n",
       "      <td>66</td>\n",
       "      <td>62</td>\n",
       "      <td>57</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>39</td>\n",
       "      <td>12</td>\n",
       "      <td>54</td>\n",
       "      <td>42</td>\n",
       "      <td>71</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>23</td>\n",
       "      <td>30</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>43</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>61</td>\n",
       "      <td>85</td>\n",
       "      <td>94</td>\n",
       "      <td>77</td>\n",
       "      <td>49</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>84</td>\n",
       "      <td>3</td>\n",
       "      <td>36</td>\n",
       "      <td>89</td>\n",
       "      <td>21</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>73</td>\n",
       "      <td>3</td>\n",
       "      <td>15</td>\n",
       "      <td>20</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  33  44  66  62  57  70\n",
       "   李四  39  12  54  42  71  10\n",
       "   王五  23  30  10  11  43  60\n",
       "2班 张三  61  85  94  77  49  25\n",
       "   李四  84   3  36  89  21  32\n",
       "   王五  66  73   3  15  20  80"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "31ab978c-3f01-48cb-ab2c-da7b60c4e243",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>33</td>\n",
       "      <td>44</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>39</td>\n",
       "      <td>12</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>23</td>\n",
       "      <td>30</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语\n",
       "张三  33  44  66\n",
       "李四  39  12  54\n",
       "王五  23  30  10"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2=df.loc['1班','期中']\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "687845bf-19c6-413b-88de-46e552b85d2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "张三    143\n",
       "李四    105\n",
       "王五     63\n",
       "dtype: int64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.sum()\n",
    "df2.sum(axis=0)# 默认求每一列中多行的和\n",
    "df2.sum(axis=1)# 求每一行中多列求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "14d8bcf9-afae-4314-b435-501f0550306b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(311)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求所有数的和\n",
    "df2.values.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "071d93bb-0464-4f1b-ae7c-31719637f650",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文    39\n",
       "数学    44\n",
       "英语    66\n",
       "dtype: int32"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.mean()\n",
    "df2.max()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae00e329-ee6b-4d4d-a09e-4944e462f6df",
   "metadata": {},
   "source": [
    "多层索引的聚合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "79c75145-c2d8-4d1b-a27c-d3763cbc7f46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  张三    332\n",
       "    李四    228\n",
       "    王五    177\n",
       "2班  张三    391\n",
       "    李四    265\n",
       "    王五    257\n",
       "dtype: int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sum() # 默认每一列中多行的和\n",
    "df.sum(axis=0)\n",
    "\n",
    "df.sum(axis=1) # 每一行中多列的和"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
